﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CRM.BLL;
using CRM.Entity;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.PhoneAreaManage
{
    public partial class SetClassify : Common.PageBase
    {
        private T_PHONEAREA_BLL phoneAreaBll = new T_PHONEAREA_BLL();
        private T_BUSINESSDIVISION_BLL businessDivisionBll = new T_BUSINESSDIVISION_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBusinessDivisionBinding();
                ddlPROVINCEBinding();
                ddlCITYBinding();

            }
        }

        private void ddlBusinessDivisionBinding()
        {
            List<T_BUSINESSDIVISION_Entity> list = businessDivisionBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBusinessDivision.DataSource = list;
            ddlBusinessDivision.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBusinessDivision.DataValueField = "BUSINESSDIVISIONID";
            ddlBusinessDivision.DataBind();
        }

        private void ddlPROVINCEBinding()
        {
            DataTable dt = phoneAreaBll.GetDistinctTable("PROVINCE", " and PROVINCE is not null ");

            ddlPROVINCE.DataSource = dt.Select("", "PROVINCE").Select(p => new
            {
                PROVINCE = p["PROVINCE"].ToString(),
                PROVINCENAME = (p["PROVINCE"].ToString().Length > 0) ? StringHelper.GetChineseSpell(p["PROVINCE"].ToString())[0].ToString() + "-" + p["PROVINCE"].ToString() : p["PROVINCE"].ToString()
                
            });
            ddlPROVINCE.DataTextField = "PROVINCENAME";
            ddlPROVINCE.DataValueField = "PROVINCE";
            ddlPROVINCE.DataBind();
            ddlPROVINCE.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlCITYBinding()
        {
            ddlCITY.Items.Clear();
            if (!String.IsNullOrEmpty(ddlPROVINCE.SelectedValue.Trim()))
            {
                DataTable dt = phoneAreaBll.GetDistinctTable("CITY", String.Format(" and CITY is not null and PROVINCE = '{0}' ", ddlPROVINCE.SelectedValue.Trim()));
                ddlCITY.DataSource = dt.Select("", "CITY").Select(p => new
                {
                    CITY = p["CITY"].ToString(),
                    CITYNAME = (p["CITY"].ToString().Length > 0) ? StringHelper.GetChineseSpell(p["CITY"].ToString())[0].ToString() + "-" + p["CITY"].ToString() : p["CITY"].ToString()
                });
                ddlCITY.DataTextField = "CITYNAME";
                ddlCITY.DataValueField = "CITY";
                ddlCITY.DataBind();
            }
            ddlCITY.Items.Insert(0, new ListItem("", ""));
        }



        protected void ddlPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCITYBinding();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string proxyName = txtProxyName.Text.Trim();
            string classify = txtCLASSIFY.Text.Trim();
            string sets = "";
            string conditions = "";

            if (!String.IsNullOrEmpty(ddlCITY.SelectedValue.Trim()))
            {
                conditions = String.Format(" and PROVINCE = '{0}' and CITY = '{1}' ", ddlPROVINCE.SelectedValue.Trim(), ddlCITY.SelectedValue.Trim());
            }
            else if (!String.IsNullOrEmpty(ddlPROVINCE.SelectedValue.Trim()))
            {
                conditions = String.Format(" and PROVINCE = '{0}'  ", ddlPROVINCE.SelectedValue.Trim());
            }
            if (String.IsNullOrEmpty(conditions))
            {
                PageHelper.ShowMessage("请选择省份！");
                return;
            }


            switch (ddlBusinessDivision.SelectedValue.Trim())
            {
                case "9":   //挖掘机事业部
                    {
                        sets = String.Format(" Classify='{0}' , ClassifyName='{1}'  ", classify, proxyName);
                    }
                    break;
                case "8":   //海外事业部
                    {
                        sets = String.Format(" Classify7='{0}' , ClassifyName7='{1}' ", classify, proxyName);
                    }
                    break;
                case "7":   //重型装备事业部
                    {
                        sets = String.Format(" Classify4='{0}' , ClassifyName4='{1}' ", classify, proxyName);
                    }
                    break;
                case "6":   //装载机事业部
                    {
                        sets = String.Format(" Classify3='{0}' , ClassifyName3='{1}' ", classify, proxyName);
                    }
                    break;
                case "4":   //农业装备事业部
                    {
                        sets = String.Format(" Classify2='{0}' , ClassifyName2='{1}' ", classify, proxyName);
                    }
                    break;
                case "3":   //五星摩托车事业部
                    {
                        sets = String.Format(" Classify6='{0}' , ClassifyName6='{1}' ", classify, proxyName);
                    }
                    break;
                case "2":  //五星汽车事业部
                    {
                        sets = String.Format(" Classify5='{0}' , ClassifyName5='{1}'  ", classify, proxyName);
                    }
                    break;
            }
            if(String.IsNullOrEmpty(sets))
            {
                PageHelper.ShowMessage("请选择事业部！");
                return;
            }
            try
            {
                phoneAreaBll.Update(sets, conditions);
                PageHelper.ShowMessage(this.Page, "更新成功！", Request.RawUrl.ToString());
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage(String.Format("更新失败！{0}", ex.Message));
            }

        }
    }
}
